package com.example.glgk.controller.IndicatorReport;


import com.example.glgk.dto.indicator.IndicatorCalculateDto;
import com.example.glgk.dto.indicator.IndicatorOverviewDto;
import com.example.glgk.dto.indicatorReport.IndicatorExplainVO;
import com.example.glgk.dto.indicatorReport.IndicatorOverAllSituationVO;
import com.example.glgk.dto.indicatorReport.IndicatorOverMonthsVO;
import com.example.glgk.dto.indicatorReport.IndicatorReportDto;
import com.example.glgk.service.indicatorReport.IndicatorReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 指标报告controller
 */
@Api(tags = "指标报告controller")
@Slf4j
@RestController
@RequestMapping("/IndicatorReportController")
@CrossOrigin(origins = "*")
public class IndicatorReportController {

    @Autowired
    IndicatorReportService indicatorReportService;

    @ApiOperation(value = "指标解释")
    @PostMapping("/getIndicatorExplain")
    public IndicatorExplainVO getIndicatorExplain(@RequestBody IndicatorReportDto indicatorReportDto) {

        IndicatorExplainVO indicatorExplainVO = indicatorReportService.getIndicatorExplain(indicatorReportDto);
        return indicatorExplainVO;
    }

    @ApiOperation(value = "指标整体情况")
    @PostMapping("/getIndicatorOverAllSituation")
    public IndicatorOverAllSituationVO getIndicatorOverAllSituation(@RequestBody IndicatorReportDto indicatorReportDto) {
        IndicatorOverAllSituationVO indicatorOverAllSituationVO = indicatorReportService.getIndicatorOverAllSituation(indicatorReportDto);
        return indicatorOverAllSituationVO;
    }


    @ApiOperation(value = "指标整体情况")
    @PostMapping("/getIndicatorMonthByYear")
    public List<HashMap<String, String>> getIndicatorMonthByYear(@RequestBody IndicatorReportDto indicatorReportDto) {
        List<HashMap<String, String>> result = new ArrayList<>();
        List<IndicatorOverMonthsVO> currentYear = indicatorReportService.getIndicatorMonthByYear(indicatorReportDto);
        indicatorReportDto.setCurYear(String.valueOf(Integer.valueOf(indicatorReportDto.getCurYear()) - 1));
        List<IndicatorOverMonthsVO> lastYear = indicatorReportService.getIndicatorMonthByYear(indicatorReportDto);
        for (int i = 0; i < 12; i++) {
            HashMap<String, String> map = new HashMap<>();
            String currentValue = currentYear.get(i).getIndicatorValue();
            String lastValue = lastYear.get(i).getIndicatorValue();
            //计算同环比
            map.put("currentValue", currentValue);
            map.put("lastValue", lastValue);
            //map.put("month", String.format("%02d", i));
            map.put("bz", getBz(currentValue, lastValue));
            result.add(map);
        }
        return result;
    }

    private static String getBz(String currentValue, String lastValue){
        if ("0".equals(lastValue)){
            return "0";
        }
        Double v = (Double.valueOf(currentValue) - Double.valueOf(lastValue)) / Double.valueOf(lastValue);
        return String.valueOf(v);
    }



    @ApiOperation(value = "指标预警科室总览")
    @PostMapping("/getIndicatorRiskOverAllSituation")
    public void getIndicatorRiskOverAllSituation(@RequestBody IndicatorReportDto indicatorReportDto) {
        indicatorReportService.getIndicatorRiskOverAllSituation(indicatorReportDto);
    }


    @ApiOperation(value = "达标科室")
    @PostMapping("/getIndicatorStandard")
    public void getIndicatorStandard(@RequestBody IndicatorReportDto indicatorReportDto) {
        indicatorReportService.getIndicatorStandard(indicatorReportDto);
    }
}
